<template>
  <div>

    <!-- 1.定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/guke' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>预约管理</el-breadcrumb-item>
      <el-breadcrumb-item>预约详情</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 2.定义卡片视图 -->
    <el-card class="box-card">

      <!-- 3.定义栅格的一行 :gutter="20"定义行间距   :span="9" 占位符  -->
      <el-row :gutter="20">
        <el-col :span="9">
          <!-- 3.定义搜索框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getroomList">
            <el-button slot="append" icon="el-icon-search" @click="getroomList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 定义添加按钮-->
          <el-button type="primary" @click="dialogVisible = true">添加订单</el-button>
        </el-col>
      </el-row>

      <!-- 客房数据表格展现  border 边框线属性  -->
      <el-table :data="roomList" border stripe>
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="roomId" label="房号"></el-table-column>
        <el-table-column prop="roomType" label="客房类型"></el-table-column>
        <el-table-column prop="sale" label="房间价格"></el-table-column>
        <el-table-column prop="people" label="人数"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            空闲<el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" disabled>
            </el-switch>已预约
          </template>
        </el-table-column>
        <el-table-column label="购买">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-s-goods" size="small" @click="addOrdersBtn(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页组件 关于分页插件的属性说明
            1.@size-change 如果分页中每页实现的条数发生变化 则会触发这个该事件
            2.@current-change 当页数发生改变时,触发该函数
            3.current-page 当前选中的页
            4.page-sizes   页面数据展现的选项
            5.page-size    默认选中的页数
            6.layout    分页中展现的数据种类
            7.total     记录总数
       -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum" :page-sizes="[5, 10, 20]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 编辑订单新增对话框 visible.sync 控制对话框的显示-->
    <el-dialog title="添加订单" :visible.sync="dialogVisible" width="50%" @close="closeDialog">

      <!-- 定义订单提交表单数据-->
      <el-form :model="addOrdersModel" :rules="rules" ref="addroomRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户姓名" prop="name">
          <el-input v-model="addOrdersModel.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="addOrdersModel.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addOrdersModel.phone"></el-input>
        </el-form-item>
        <el-form-item label="身份证号码" prop="number">
          <el-input v-model="addOrdersModel.number"></el-input>
        </el-form-item>
        <el-form-item label="预约时间" required>
          <el-col :span="11">
            <el-form-item prop="checkIn">
              <el-date-picker v-model="addOrdersModel.date" type="daterange" format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd" align="right" unlink-panels range-separator="至" start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form-item>
        <el-form-item label="入住人数" prop="person">
          <el-input-number v-model="addOrdersModel.person" :min="1" :max="4" label="几个人"></el-input-number>
        </el-form-item>
        <el-form-item label="客房类型" prop="type">
          <el-select v-model="addOrdersModel.type" placeholder="请选择客房类型">
            <el-option label="标间1 100元" value="标间1"></el-option>
            <el-option label="标间2 100元" value="标间2"></el-option>
            <el-option label="标间3 100元" value="标间3"></el-option>
            <el-option label="标间4 100元" value="标间4"></el-option>
            <el-option label="标间5 100元" value="标间5"></el-option>
            <el-option label="标间6 100元" value="标间6"></el-option>
            <el-option label="双人间1 150元" value="双人间1"></el-option>
            <el-option label="双人间2 150元" value="双人间2"></el-option>
            <el-option label="双人间3 150元" value="双人间3"></el-option>
            <el-option label="双人间4 150元" value="双人间4"></el-option>
            <el-option label="双人间5 150元" value="双人间5"></el-option>
            <el-option label="双人间6 150元" value="双人间6"></el-option>
            <el-option label="开黑房1 300元" value="开黑房1"></el-option>
            <el-option label="开黑房2 300元" value="开黑房2"></el-option>
            <el-option label="开黑房3 300元" value="开黑房3"></el-option>
            <el-option label="开黑房4 300元" value="开黑房4"></el-option>
            <el-option label="开黑房5 300元" value="开黑房5"></el-option>
            <el-option label="开黑房6 300元" value="开黑房6"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addroomBtn">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    data() {
      //校验身份证号码规则 rule校验规则   value校验的数据   callback回调函数
      const checkNumber = (rule, value, callback) => {
        //定义身份证号码的正则表达式  JS中用/来表示正则表达式的开始和结束
        const numberRege = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        //正则表达式语法校验  test(xx) 校验成功 返回true  校验失败返回false
        if (numberRege.test(value)) {
          //表示身份证号码合法 正确返回
          return callback()
        }
        callback(new Error('请填写正确的身份证号码'))
      }

      //校验手机号的身份证号码规则
      const checkPhone = (rule, value, callback) => {

        //定义校验手机号的正则语法
        const phoneRege = /^1[34578][0-9]{9}$/
        if (phoneRege.test(value)) {
          return callback()
        }
        callback(new Error('请填写正确的手机号'))
      }

      return {
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 5
        },
        roomList: [],
        //记录总数是数值类型
        total: 0,
        dialogVisible: false,
        addOrdersModel: {
          date: '',
          name: '',
          sex: '',
          phone: '',
          number: '',
          checkIn: '',
          checkOut: '',
          status: false,
          type: '',
          person: ''
        },
        //数据校验规则
        rules: {
          name: [{
              required: true,
              message: '请输入用户姓名',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 30,
              message: '用户姓名不可以为空',
              trigger: 'blur'
            }
          ],
          type: [{
              required: true,
              message: '请输入客房名称',
              trigger: 'blur'
            },
            {
              min: 2,
              max: 30,
              message: '客房名不能为空',
              trigger: 'blur'
            }
          ],
          sex: [{
              required: true,
              message: '请输入性别',
              trigger: 'blur'
            },
            {
              message: '性别不能为空',
              trigger: 'blur'
            }
          ],
          phone: [{
              required: true,
              message: '请输入手机号',
              trigger: 'blur'
            },
            {
              min: 11,
              max: 11,
              message: '长度在 3 到 11 个字符',
              trigger: 'blur'
            },
            {
              validator: checkPhone,
              trigger: 'blur'
            }
          ],
          number: [{
              required: true,
              message: '请输入身份证号码',
              trigger: 'blur'
            },
            {
              min: 18,
              max: 18,
              message: '长度在18个字符',
              trigger: 'blur'
            },
            {
              validator: checkNumber,
              trigger: 'blur'
            }
          ]
        }
      }
    },
    methods: {
      async getroomList() {
        const {
          data: result
        } = await this.$http.get('/room/getRoomList', {
          params: this.queryInfo
        })
        if (result.status !== 200) return this.$message.error("客房列表查询失败")
        this.roomList = result.data.rows
        this.total = result.data.total
        console.log("总记录数:" + this.total)
      },
      //添加订单数据
      addroomBtn() {
        this.$refs.addroomRef.validate(async valid => {
          //如果校验失败 则停止数据
          if (!valid) return
          let startDate = this.addOrdersModel.date.toString();
          this.addOrdersModel.checkIn = startDate.substring(0, 10);
          this.addOrdersModel.checkOut = startDate.substring(11);
          //console.log(this.addOrdersModel)
          const {
            data: result
          } = await this.$http.post('/temporary/addTemporary', this.addOrdersModel)
          if (result.status !== 200) return this.$message.error("订单新增失败")
          this.$message.success("订单新增成功")
          //关闭对话框
          this.dialogVisible = false
          //重新获取员工列表
          this.getroomList()
          this.$router.push("/sub/sure")
        })
      },
      handleSizeChange(pageSize) {
        //console.log("每页展现的条数"+pageSize)
        this.queryInfo.pageSize = pageSize
        this.getroomList()
      },
      handleCurrentChange(pageNum) {
        //console.log("页数:"+pageNum)
        this.queryInfo.pageNum = pageNum
        this.getroomList()
      },
      closeDialog() {
        //重置表格数据
        this.$refs.addroomRef.resetFields()
      }
    },
    //利用钩子函数实现数据查询
    mounted() {
      this.getroomList()
    }
  }
</script>


<style lang="less" scoped>

</style>
